<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>新增订单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>

<body>

    <div class="layuimini-container">
        <div class="layuimini-main">
            <form class="layui-form layui-form-pane" action="">
                <fieldset>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">系统</label>
                        <div class="layui-input-block">
                            <select name="system" lay-verify="required">
                                <option value="">请选择系统</option>
                                <option value="安卓" selected>安卓</option>
                                <option value="iOS">iOS</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">类型</label>
                        <div class="layui-input-block">
                            <select name="type" lay-verify="required">
                                <option value="">请选择类型</option>
                                <option value="QQ">QQ</option>
                                <option value="微信">微信</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">外站单号</label>
                        <div class="layui-input-block">
                            <input type="text" name="externalId" placeholder="请输入外站单号（可选）" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">订单数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="orderCount" placeholder="请输入订单数量" value="1" class="layui-input" min="1">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">积分数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="totalPoints" placeholder="请输入订单总积分数量" value="" class="layui-input" min="1" lay-verify="required">
                        </div>
                    </div>



                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <br>

                    <button class="layui-btn" lay-submit value="保存订单" lay-filter="saveBtn">保存订单</button>
                </fieldset>
            </form>
        </div>
    </div>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/lib/axios/axios.js" charset="utf-8"></script>
    <script>

        layui.use(['form'], function () {
            const $ = layui.jquery;
            const form = layui.form;
            const layer = layui.layer;
            
            // 获取父页面传递的数据
            let parentData = parent.data;
            if (!parentData) {
                layer.alert("未获取到父页面数据");
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            }

            // 表单验证规则
            form.verify({
                // 可以在这里添加自定义验证规则
            });

            // 监听提交
            form.on('submit(saveBtn)', function (data) {
                let formData = data.field;

                // 表单验证
                if (!formData.system || !formData.type || !formData.orderCount || !formData.totalPoints) {
                    return layer.msg('请填写完整信息！');
                }
                
                // 转换数据类型
                formData.orderCount = parseInt(formData.orderCount);
                formData.totalPoints = parseInt(formData.totalPoints);

                // 提交数据
                axios.post('/order/add', formData)
                    .then(function (res) {
                        const resData = res.data;
                        if (resData.code == 0) {
                            layer.msg("新增成功", {icon: 1});
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.msg(resData.msg || '新增失败', {icon: 2});
                        }
                    })
                    .catch(function (err) {
                        console.log("新增订单失败", err);
                        layer.msg('新增失败：' + (err.response?.data?.msg || err.message), {icon: 2});
                    });
                return false;
            });
        });
    </script>

</body>

</html>